package com.tsu.mall.common.mapper;

import com.tsu.mall.common.VO.PersonalVO;
import com.tsu.mall.common.entity.Bill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tsu.mall.common.entity.Cart;
import com.tsu.mall.common.entity.MemberAddress;
import com.tsu.mall.common.entity.Wares;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author tttttt
 * @since 2023-03-30
 */
public interface BillMapper extends BaseMapper<Bill> {

    List<Bill> doselectList(String accountNumber, String code,String billStatus);


    @Update("update bill set status='已发货' where id=#{id}")
    void doUpdateStatus(Integer id);

    @Select("select * from cart where id=#{cartId}")
    Cart selectByCartId(Integer cartId);

    @Select("select * from wares where id=#{waresId}")
    Wares doSelectById(Integer waresId);

    @Select("select max(serial_number) from bill where instr('serial_number',#{pre})=1")
    String selectMaxCode(String pre);

    @Delete("delete from cart where id=#{cartId}")
    void deleteByCartId(int cartId);

    @Select("select * from member_address where id=#{memberAddressId}")
    MemberAddress selectByAddressId(Integer memberAddressId);

    @Select("select * from bill where member_id=#{id}")
    List<Bill> doList(Integer id);

    @Delete("delete from bill_detail where bill_id=#{billId}")
    void deleteBillDetailById(Integer billId);
    @Delete("delete from bill_address where bill_id=#{billId}")
    void deleteBillAddressById(Integer billId);

    @Select("select * from bill where status=#{status}")
    List<Bill> selectByStatus(String status);

    @Select("SELECT \n" +
            "    COUNT(CASE WHEN status = '待支付' THEN 1 END) AS statusNoPayAmount,\n" +
            "    COUNT(CASE WHEN status = '待收货' THEN 1 END) AS statusNoReceiveAmount,\n" +
            "    COUNT(CASE WHEN status = '待评价' THEN 1 END) AS statusNoEvaAmount\n" +
            "FROM \n" +
            "    bill_detail\n" +
            "where bill_id in\n" +
            "(select id\n" +
            "FROM\n" +
            "bill\n" +
            "where member_id=#{memberId}) ")
    PersonalVO doSelectByStatus(Integer memberId);

    @Select("select id from bill where code=#{billCode}")
    Integer doSelectByCode(String billCode);

    @Select("select * from bill where id=#{id}")
    Bill doSelectBillById(Integer id);

    @Update("update bill set status='已取消' where id=#{id}")
    void updateBillStatusById(Integer id);
    @Update("update bill_detail set status='已取消' where bill_id=#{id}")
    void updateBillDetailStatusById(Integer id);
}
